package io.dy.countDownLatch;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : dmj
 * @date: 2024/8/15 2:07 下午
 * @description: 运用 CountDownLatch 模拟LOL游戏玩家加载游戏过程
 */
public class Load {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(10);
        ExecutorService service = Executors.newFixedThreadPool(10);
        String[] loadStr = new String[10];
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            int k = i;
            service.execute(()->{
                for (int j = 0; j <= 100; j++) {
                    try {
                        Thread.sleep(random.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    loadStr[k] = j + "%";
                    System.out.print("\r" + Arrays.toString(loadStr));
                }
                latch.countDown();
            });
        }
        latch.await();
        System.out.println("\n全部玩家加载完成，游戏开始。。。");
        service.shutdown();
    }
}
